//数据组件
//用户
<template>
    <div>
    <div class="demo-input-size" style="margin-bottom: 20px">
  <el-button size="small" style="margin-bottom: 5px;margin-left: 20px;background-color: #BEBEBE" @click="add">新增</el-button>
</div>
    <el-table :data="tableData"
    :header-cell-style="{background:'#3DC5A1',color:'#555'}"
    :border="true">
      <el-table-column prop="questionaireId" label="问卷编号" width="250">
      </el-table-column>
      <el-table-column prop="title" label="标题" width="250">
      </el-table-column>
      <el-table-column prop="introduction" label="简介" width="250">
      </el-table-column>
      <el-table-column label="题目" width="250">
      <template #default="scope">
          <el-button size="small" @click="read(scope.row.questionaireId)">查看题目</el-button>
      </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="190">
      <template #default="scope">
        <el-popconfirm
          title="确定删除吗？"
          @confirm="del(scope.row.questionaireId)"
          style="margin-left:5px">
          <el-button slot="reference" size="small" table="delete" >删除</el-button>
          </el-popconfirm>
            <el-button size="small" table="edit" @click="mod(scope.row.questionaireId)">修改</el-button>
      </template>
                 
      </el-table-column>
      </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[7, 10, 12]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    
    </div>
</template>

<script>
export default {
  name: "USer",
  mounted() {
    this.loadPost(); // 确保在组件挂载后加载数据
},
  data() {
      return {
        tableData:[],
        pageNum:1,
        pageSize:7,
        total:1,
        questionaireId:"",
      }
    },
    methods: {
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageNum=1;
        this.pageSize=val;
        this.loadPost();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pageNum=val;
        this.loadPost();
      },
      loadGet(){
          this.$axios.get('http://localhost:8082/questionaire/list').then(res=>res.data).then(res=>{
              console.log(res);
              this.tableData = res;
          })
      },
      loadPost(){
            this.$axios.post('http://localhost:8082/questionaire/listPage',{
              pageNum:this.pageNum,
              pageSize:this.pageSize,
              total:this.total
            }).then(res=>res.data).then(res=>{
                console.log(res)
                if (res.code==200){
                    this.tableData = res.data;
                    this.total=res.total;
                    
                }else{
                    alert("获取失败")
                }
            })
        },
        del(questionaireId) {
  console.log(questionaireId);
  this.$axios.delete(`http://localhost:8082/questionaire/${questionaireId}`).then(res => {
    console.log(res.data);
    if (res.data.code == 200) {
      this.$message({
        message: '删除成功'
      });
      this.loadPost(); // 假设这是重新加载列表的方法
    } else {
      this.$message({
        message: '删除失败'
      });
    }
  }).catch(error => {
    console.error('Error:', error);
    this.$message({
      message: '删除失败'
    });
  });
},

        mod(){
            console.log("修改")
            this.$nextTick(()=>{
            this.resetForm()
          })
            //复制到表单
          
        },
        add(){
          this.centerDialogVisible=true
          this.$nextTick(()=>{
            this.resetForm()
          })
        },
        read(questionaireId){
          console.log(questionaireId)
          this.$router.push('/questionaire/'+questionaireId);
        },
  }
}
</script>